#!/usr/bin/env sh

# Script to deploy certificates to Tencent CDN

# The following variables exported from environment will be used.
# If not set then values previously saved in domain.conf file are used.
#
#
# export DEPLOY_TENCENT_CDN_ID=""  # access key id
# export DEPLOY_TENCENT_CDN_SECRET=""  # access key secret
# export DEPLOY_TENCENT_CDN_DOMAINS=""  # domains, used for SAN or wildcard certificate
 # export DEPLOY_TENCENT_CDN_MULTI=1 # whether use multi account mode

########  Public functions #####################
tencent_cdn_m_deploy() {
  _cdomain="$1"
  _ckey="$2"
  _ccert="$3"
  _cca="$4"
  _cfullchain="$5"

  _debug _cdomain "$_cdomain"
  _debug _ckey "$_ckey"
  _debug _ccert "$_ccert"
  _debug _cca "$_cca"
  _debug _cfullchain "$_cfullchain"

  # whether use multi Tencent account.
  _getdeployconf DEPLOY_TENCENT_CDN_MULTI
  _debug2 DEPLOY_TENCENT_CDN_MULTI "$DEPLOY_TENCENT_CDN_MULTI"
  _savedeployconf DEPLOY_TENCENT_CDN_MULTI "$DEPLOY_TENCENT_CDN_MULTI"

  _tencent_cdn_m_deploy
  _err_code="$?"
  return $_err_code
}

_tencent_cdn_m_deploy() {
  _err_code=1
  # if not define DEPLOY_ALI_CDN_MULTI, break the loop
  if [ -z "$DEPLOY_ALI_CDN_MULTI" ]; then
      fix='null'
  elif
      fix='null _0 _1 _2 _3 _4 _5 _6 _7 _8 _9'
  fi

  for suffix in `echo $fix`;do
    _info suffix $suffix

    if [ "$suffix" == "null" ];then
      suffix=''
    fi

    # access key id is required to access Ali api. 
    _getdeployconf DEPLOY_TENCENT_CDN_ID$suffix
    eval TMP_VAL="\$DEPLOY_TENCENT_CDN_ID$suffix" 
    _debug2 DEPLOY_TENCENT_CDN_ID$suffix "$TMP_VAL"
    if [ -z "$TMP_VAL" ]; then
      _info "DEPLOY_TENCENT_CDN_ID$suffix not defined."
      continue
    fi
    _savedeployconf DEPLOY_TENCENT_CDN_ID$suffix "$TMP_VAL" 
    DEPLOY_TENCENT_CDN_ID="$TMP_VAL"

    # access key secret is required to access Ali api. 
    _getdeployconf DEPLOY_TENCENT_CDN_SECRET$suffix
    eval TMP_VAL="\$DEPLOY_TENCENT_CDN_SECRET$suffix" 
    _debug2 DEPLOY_TENCENT_CDN_SECRET$suffix "$TMP_VAL"
    if [ -z "$TMP_VAL" ]; then
      _info "DEPLOY_TENCENT_CDN_SECRET$suffix not defined."
      continue
    fi
    _savedeployconf DEPLOY_TENCENT_CDN_SECRET$suffix "$TMP_VAL" 
    DEPLOY_TENCENT_CDN_SECRET="$TMP_VAL"

    # domain names is required when deploy SAN or wildcard cert. 
    _getdeployconf DEPLOY_TENCENT_CDN_DOMAINS$suffix
    eval TMP_VAL="\$DEPLOY_TENCENT_CDN_DOMAINS$suffix" 
    _debug2 DEPLOY_TENCENT_CDN_DOMAINS$suffix "$TMP_VAL"
    if [ -z "$TMP_VAL" ]; then
      TMP_VAL=$_cdomain
    fi
    _savedeployconf DEPLOY_TENCENT_CDN_DOMAINS$suffix "$TMP_VAL" 
    #DEPLOY_TENCENT_CDN_DOMAINS="$TMP_VAL"
    _adomains="$TMP_VAL"
    
    _debug $DEPLOY_TENCENT_CDN_ID
    _debug $DEPLOY_TENCENT_CDN_SECRET
    _debug $_adomains

    _tencent_cdn_deploy

    _err_code="$?"
    if [ "$_err_code" -ne 0 ];then
      return $_err_code
    fi
  done
  return $_err_code
}

_tencent_cdn_deploy() {
  cert=`cat "$_cfullchain"`                               
  key=`cat "$_ckey"`
  for domain in $(echo "$_adomains" | tr ',' " ");do
    tccli cdn UpdateDomainConfig --cli-unfold-argument \
    --secretId $DEPLOY_TENCENT_CDN_ID --secretKey $DEPLOY_TENCENT_CDN_SECRET \
    --Domain "$domain" --Https.Switch on --HttpsBilling.Switch on \
    --Https.CertInfo.Certificate "$cert" --Https.CertInfo.PrivateKey "$key"

    _err_code="$?"
    if [ "$_err_code" -ne 0 ];then
      return $_err_code
    fi
  done

  return 0
}
